﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace NFinal.Sql.Like
{
    public class LikeEncodeKey
    {
        public byte[] enOneByteDic;
        public byte[] deOneByteDic;

        public byte[] en6BitDic;
        public byte[] de6BitDic;

        public byte[] en5BitDic;
        public byte[] de5BitDic;

        public byte[] en4BitDic;
        public byte[] de4BitDic;

        public byte[] en3BitDic;
        public byte[] de3BitDic;

        public byte[] en2BitDic;
        public byte[] de2BitDic;

        public byte[] en1BitDic;
        public byte[] de1BitDic;

        public static byte[] GenerateRandomArray(int n,bool ignoreControlChar=false)
        {
            if (n <= 0)
                throw new ArgumentException("N 必须是正整数", nameof(n));

            byte[] array = new byte[n];
            for (int i = 0; i < n; i++)
            {
                array[i] = (byte)i;
            }

            // 使用 Fisher-Yates 洗牌算法随机排序
            Random random = new Random();

            for (int i = n - 1; i > 0; i--)
            {
                // 生成 0 到 i 之间的随机索引
                int j = random.Next(i);
                if (ignoreControlChar)
                {
                    if (i < 32 || i == 127 || i == '%' || i == '_')
                    {
                        if (i > 127)
                        {

                        }
                        continue;

                    }
                    if (j < 32 || j == 127 || j == '%' || j == '_')
                    {
                        if (i > 127)
                        {

                        }
                        continue;
                    }
                }
                byte tmp = array[i];
                array[i] = array[j];
                array[j] = tmp;
            }

            return array;
        }
        public static byte[] GetReserveRandomArray(byte[] array)
        {
            byte[] buffer = new byte[array.Length];
            for (int i = 0; i < array.Length; i++)
            {
                buffer[array[i]] = (byte)i;
            }
            return buffer;
        }
        private string keyFilePath;
        public void Save()
        {
            this.keyFilePath = System.IO.Path.Combine(System.AppContext.BaseDirectory, "key.txt");
            using (FileStream fileStream = new FileStream(this.keyFilePath, FileMode.Create))
            {
                fileStream.Write(enOneByteDic, 0, enOneByteDic.Length);
                fileStream.Write(deOneByteDic, 0, deOneByteDic.Length);
                fileStream.Write(en6BitDic, 0, en6BitDic.Length);
                fileStream.Write(de6BitDic, 0, de6BitDic.Length);
                fileStream.Write(en5BitDic, 0, en5BitDic.Length);
                fileStream.Write(de5BitDic, 0, de5BitDic.Length);
                fileStream.Write(en4BitDic, 0, en4BitDic.Length);
                fileStream.Write(de4BitDic, 0, de4BitDic.Length);
                fileStream.Write(en3BitDic, 0, en3BitDic.Length);
                fileStream.Write(de3BitDic, 0, de3BitDic.Length);
                fileStream.Flush();
                fileStream.Close();
            }
        }

        public void Load()
        {
            this.keyFilePath = System.IO.Path.Combine(System.AppContext.BaseDirectory, "key.txt");
            using (FileStream fileStream = new FileStream(this.keyFilePath, FileMode.Open))
            {
                enOneByteDic = new byte[95];
                deOneByteDic = new byte[95];
                fileStream.Read(enOneByteDic, 0, enOneByteDic.Length);
                fileStream.Read(deOneByteDic, 0, deOneByteDic.Length);
                en6BitDic = new byte[64];
                de6BitDic = new byte[64];
                fileStream.Read(en6BitDic, 0, en6BitDic.Length);
                fileStream.Read(de6BitDic, 0, de6BitDic.Length);
                en5BitDic = new byte[32];
                de5BitDic = new byte[32];
                fileStream.Read(en5BitDic, 0, en5BitDic.Length);
                fileStream.Read(de5BitDic, 0, de5BitDic.Length);
                en4BitDic = new byte[16];
                de4BitDic = new byte[16];
                fileStream.Read(en4BitDic, 0, en4BitDic.Length);
                fileStream.Read(de4BitDic, 0, de4BitDic.Length);
                en3BitDic = new byte[8];
                de3BitDic = new byte[8];
                fileStream.Read(en3BitDic, 0, en3BitDic.Length);
                fileStream.Read(de3BitDic, 0, de3BitDic.Length);
                fileStream.Close();
            }
        }
        public void GenerateKey()
        {
            enOneByteDic = GenerateRandomArray(95,true);
            deOneByteDic = GetReserveRandomArray(enOneByteDic);

            en6BitDic = GenerateRandomArray(64);
            de6BitDic = GetReserveRandomArray(en6BitDic);

            en5BitDic = GenerateRandomArray(32);
            de5BitDic = GetReserveRandomArray(en5BitDic);

            en4BitDic = GenerateRandomArray(16);
            de4BitDic = GetReserveRandomArray(en4BitDic);

            en3BitDic = GenerateRandomArray(8);
            de3BitDic = GetReserveRandomArray(en3BitDic);
        }
        public LikeEncodeKey(string filePath)
        {
            this.keyFilePath = filePath;
            if (File.Exists(filePath))
            {
                this.Load();
            }
            else
            {
                GenerateKey();
                Save();
            }

        }
    }
}
